﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;

namespace GE_Management_Tool.Planning
{
    public partial class frmA0015_List_All_Child_Task : frmBase
    {
        CResources m_parentTasks;
        IList<CResources> m_lsProject_Staff;
        CResources m_objSelected;
        IList<CProject_Staff> m_lsStaff;

        public CResources Show_To_Display(CResources p_Tasks)
        {
            m_parentTasks = p_Tasks;
            this.ShowDialog();
            return m_objSelected;
        }

        public frmA0015_List_All_Child_Task()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
            this.grvData.OptionsBehavior.Editable = true;
        }

        private void frmA0015_Add_New_Tasks_Load(object sender, EventArgs e)
        {
            try
            {
                Init_Data();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);;
            }
        }

        private void Init_Data()
        {
            m_lsProject_Staff = new List<CResources>();
            CResources_Controller v_ctrlStaff = new CResources_Controller();
            //m_lsProject_Staff = v_ctrlStaff.A001_sp_sel_List_Pro_Tech_Art_By_Project_ID(CConfig.g_intGem_Tools_Current_Project);
            m_lsProject_Staff = v_ctrlStaff.A0011_sp_sel_List_Resources_By_Project_ID(CConfig.g_intGem_Tools_Current_Project);
            cTasksBindingSource.DataSource = m_lsProject_Staff;

            //----------------------------------------------------------------------
            m_lsStaff = new List<CProject_Staff>();
            CProject_Staff_Controller v_objCtrlStaff = new CProject_Staff_Controller();
            m_lsStaff = v_objCtrlStaff.A001_List_Project_Staff_By_Project_ID_And_Regency_ID(CConfig.g_intGem_Tools_Current_Project,
                                                                                                    CUtility.Convert_To_Int32(EStaff_Type.Artist));
            lkeStaff.DataSource = m_lsStaff;
        }

        private string Check_Data()
        {
            StringBuilder v_Sb = new StringBuilder();
            if (m_objSelected.Staff_ID <= 0)
            {
                v_Sb.AppendLine("Missing Staff Name.");
            }

            return v_Sb.ToString();
        }

        private void grvData_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                m_objSelected.Project_ID = CConfig.g_intGem_Tools_Current_Project;
                m_objSelected.Parent_ID = m_parentTasks.Auto_ID;

                string error = Check_Data();
                if (error != "")
                {
                    throw new Exception(error);
                }
                //lúc này là mới thêm vào cho nên Auto ID  = 0;
                if (m_objSelected.Auto_ID <= 0)
                {
                    CResources_Controller v_ctrlStaff = new CResources_Controller();
                    m_objSelected.Auto_ID = v_ctrlStaff.Insert_Resources(m_objSelected);
                    XtraMessageBox.Show("Insert Success !");
                }
                else
                {
                    // ở đây là thêm vào hệ thống tự sinh ra auto_id rồi nên auto_id chắc chắn > 0;
                    CResources_Controller v_ctrlStaff = new CResources_Controller();
                    v_ctrlStaff.Update_Resources(m_objSelected);
                    XtraMessageBox.Show("Update Success !");
                }

                Init_Data();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grdData_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                if (XtraMessageBox.Show("Are you sure you want to delete this row ?", "Warning", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    CTasks_Controller v_ctrlStaff = new CTasks_Controller();
                    v_ctrlStaff.Delete_Tasks(m_objSelected.Auto_ID, CSession.Active_User_Name);
                }
            }
        }

        private void grvData_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objSelected = (CResources)grvData.GetRow(grvData.FocusedRowHandle);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}
